package com.imooc.LinkedList.removeLinkedListElements;

public class ListNode {

    int val;
    ListNode next;
    ListNode(int val){
        this.val = val;
    }

    // 构造函数，通过传入数组构建链表
    public ListNode(int[] arr) {
        if(arr == null || arr.length == 0) {
            throw new IllegalArgumentException("Arr cant be empty.");
        }

        this.val = arr[0];
        ListNode cur = this;
        for(int i = 1; i < arr.length; i++) {
            cur.next = new ListNode(arr[i]);
            cur = cur.next;
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        ListNode cur = this;
        while(cur != null) {
            sb.append(cur.val + "->");
            cur = cur.next;
        }
        sb.append("null");
        return sb.toString();
    }

}
